using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace BlogExchange
{
    internal class Reader
    {
        internal static List<Entry> Load(string url)
        {
            List<Entry> entries = new List<Entry>();
            XmlTextReader reader = new XmlTextReader(url);
            XmlDocument rssDocument = new XmlDocument();

            rssDocument.Load(reader);

            XmlNodeList itemNodes = rssDocument.SelectNodes("//rss/channel/item");

            foreach (XmlNode itemNode in itemNodes)
            {
                Entry entry = new Entry();

                foreach (XmlNode childNode in itemNode.ChildNodes)
                {
                    switch(childNode.Name)
                    {
                        case "title":
                            entry.Title= childNode.InnerText;
                            break;
                        case "description":
                            entry.Description = childNode.InnerText;
                            break;
                        case "link":
                            entry.Link = childNode.InnerText;
                            break;
                        case "author":
                            entry.Author = childNode.InnerText;
                            break;
                        case "pubDate":
                            DateTime pubDate = DateTime.Now;
                            DateTime.TryParse(childNode.InnerText, out pubDate);
                            entry.Date = pubDate;
                            break;
                    }
                }

                entries.Add(entry);
            }

            return entries;
        }

        

    }
}
